package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.dongdongshop.dataConfig.ResponseStatus;
import com.dongdongshop.dataConfig.dataResult;
import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.pojo.TbContentCategory;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.service.ContentCategoryService;
import com.dongdongshop.service.ContentService;
import com.dongdongshop.util.PageBean;
import org.apache.shiro.crypto.hash.Hash;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("content")
public class ContentController {
    private Logger logger  = LoggerFactory.getLogger(ContentController.class);
    @Reference
    private ContentService contentService;
    @Reference
    private ContentCategoryService contentCategoryService;

    @RequestMapping("getinfo")
    @ResponseBody
    public dataResult getinfo(@RequestParam(defaultValue = "1")Integer number,@RequestParam(defaultValue = "7")Integer pageSize){
        try {
            PageBean<TbContent> b = contentService.getinfo(number,pageSize);
            List<TbContentCategory> list = contentCategoryService.getinfo();
            Map<String,Object> map = new HashMap<>();
            map.put("b",b);
            map.put("list",list);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName(),map);
        }catch (Exception e){
            logger.error("查询失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("toAdd")
    @ResponseBody
    public dataResult toAdd(TbContent tbContent){
        try {
            Integer i = contentService.toAdd(tbContent);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName());
        }catch (Exception e){
            logger.error("查询失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("deleteAll")
    @ResponseBody
    public dataResult deleteAll(@RequestParam("ids[]")Long[] ids){
        try {
            Integer i = contentService.deleteAll(ids);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName());
        }catch (Exception e){
            logger.error("查询失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("toUpdate")
    @ResponseBody
    public dataResult toUpdate(Long id, Model model){
        try {
            TbContent tbContent = contentService.toUpdate(id);
            List<TbContentCategory> list = contentCategoryService.getinfo();
            Map<String,Object> map = new HashMap<>();
            map.put("t",tbContent);
            map.put("list",list);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName(),map);
        }catch (Exception e){
            logger.error("查询失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }

    @RequestMapping("updateinfo")
    @ResponseBody
    public dataResult updateinfo(TbContent tbContent){
        try {
            Integer i = contentService.updateinfo(tbContent);
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName());
        }catch (Exception e){
            logger.error("查询失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }


    //将图片上传到阿里云上
    @RequestMapping("uploadFile")
    @ResponseBody
    public dataResult uploadFile (@RequestParam(value = "myFile") MultipartFile myFile){
        try {
            //阿里云上传图片sdk
            // Endpoint以杭州为例，其它Region请按实际情况填写。
            String endpoint = "https://oss-cn-beijing.aliyuncs.com";
            String domain = "oss-cn-beijing.aliyuncs.com";
            // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录RAM控制台创建RAM账号。
            String accessKeyId = "LTAI4GEGkEzP3TXfWZX4uBE1";
            String accessKeySecret = "HMBAjZTWfd2o6SWMs2NoyNDVt9ONAe";
            String bucketName = "lashibudaizhi";
            // <yourObjectName>上传文件到OSS时需要指定包含文件后缀在内的完整路径，例如abc/efg/123.jpg。
            String objectName = myFile.getOriginalFilename();

            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            String content = "Hello OSS";
            // 上传文件到指定的存储空间（bucketName）并将其保存为指定的文件名称（objectName）。
            ossClient.putObject(bucketName, objectName, myFile.getInputStream());
            // 关闭OSSClient。
            String url = "https://"+bucketName+"."+domain+"/"+objectName;
            ossClient.shutdown();
            return new dataResult(ResponseStatus.SUCCESS.getIndex(), ResponseStatus.SUCCESS.getName(),url);
        }catch (Exception e){
            logger.error("查询失败",e);
            return new dataResult(ResponseStatus.SERVER_ERROR.getIndex(),ResponseStatus.SERVER_ERROR.getName());
        }
    }
}
